﻿// Developer: Cory Fowler
// Company: Innosphere SDG

namespace WebRole1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using WebRole1.LittleBlackBook;
    using WebRole1.LittleBlackBook.Data;

    public partial class Bets : System.Web.UI.Page
    {
        LittleBlackBook.Data.LittleBlackBookEntities lbbContext;
        Uri svrUrl = new Uri("http://littleblackbookdb.cloudapp.net/LittleBlackBookService.svc/");

        protected void Page_Load(object sender, EventArgs e)
        {
            lbbContext = new WebRole1.LittleBlackBook.Data.LittleBlackBookEntities(svrUrl);

            if (!Page.IsPostBack)
            {
                foreach (var person in lbbContext.people)
                {
                    _person.Items.Add(new ListItem(person.lname + ", " + person.fname, person.ID.ToString()));
                }
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            int personId = 0;
            Int32.TryParse(_person.SelectedValue, out personId);

            try
            {
                BetList.DataSource = (from b in lbbContext.bets where b.person_id == personId select b);
                BetList.DataBind();
            }
            catch
            {

            }
        }

        protected void BetList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView grid = sender as GridView;

            if (grid != null)
            {
                HiddenField betIdField = grid.Rows[e.RowIndex].FindControl("id") as HiddenField;

                if (betIdField != null)
                {
                    int betId = 0;
                    Int32.TryParse(betIdField.Value, out betId);

                    DeleteBet(betId);
                }
            }
            e.Cancel = true;
        }

        protected void BetList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridView grid = sender as GridView;

            if (grid != null)
            {
                HiddenField betIdField = grid.Rows[e.NewSelectedIndex].FindControl("id") as HiddenField;

                if (betIdField != null)
                {
                    int betId = 0;
                    Int32.TryParse(betIdField.Value, out betId);

                    FillBetForEdit(betId);
                }
            }
            e.Cancel = true;
        }

        private void DeleteBet(int betId)
        {
            try
            {
                bets bet = GetBet(betId);

                lbbContext.DeleteObject(bet);
                lbbContext.SaveChanges();
            }
            catch
            {
                // Inform User of Failure
            }
        }

        private void FillBetForEdit(int betId)
        {
            bets bet = GetBet(betId);

            _id.Value = bet.id.ToString();
            _person.SelectedValue = bet.person_id.ToString();
            _event.Text = bet.@event;
            _value.Text = bet.value.ToString();
        }

        private bets GetBet(int betId)
        {
            try
            {
                return (from b in lbbContext.bets
                        where b.id == betId
                        select b).FirstOrDefault();
            }
            catch
            {
                return null;
            }

        }

        protected void Save_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int betId = 0;
                Int32.TryParse(_id.Value, out betId);

                int personId = 0;
                Int32.TryParse(_person.SelectedValue, out personId);

                double value = 0.0;
                Double.TryParse(_value.Text, out value);

                SaveBet(betId, personId, value);
                BetList.DataBind();
                ClearForm();
            }
        }

        private void SaveBet(int betId, int personId, double value)
        {
            bets bet = GetBet(betId) ??
                       new bets();

            bet.person_id = personId;
            bet.value = value;
            bet.@event = HttpUtility.HtmlEncode(_event.Text);

            // Add bet to be Updated, Save changes to send UPDATE.
            try
            {
                if (betId == 0)
                    lbbContext.AddTobets(bet);
                else
                    lbbContext.UpdateObject(bet);
                lbbContext.SaveChanges();
            }
            catch
            {
                // Inform User of Failure
            }
        }

        private void ClearForm()
        {
            _id.Value = string.Empty;
            _person.ClearSelection();
            _event.Text = string.Empty;
            _value.Text = string.Empty;
        }
    }
}